﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OBUCare.Core.Common;
using OBUCare.Web.Infrastructure;

namespace OBUCare.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            System.Web.Security.FormsAuthentication.SignOut();
            if (System.Web.HttpContext.Current.Session["userInfo"] != null)
                System.Web.HttpContext.Current.Session["userInfo"] = null;
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        /// <summary>
        /// 登录函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns>成功，返回空提示</returns>
        public ActionResult Login(string name, string pwd)
        {
            bool result = false;
            string message = "";
            string defaultUrl = "";
            try
            {
                int.Parse(name);

                result = UIUtility.Login(name, pwd);
                if (!result)
                    message = "登录失败，用户名或密码不正确，请重试！";
                else
                    defaultUrl = UIUtility.GetDefaultPage();

                if (defaultUrl == "")
                    message = "无权限访问此系统，请与管理员联系";
            }
            catch (FormatException ex)
            {
                TraceHelper.TraceError("用户登录:",ex);
                message = "登录失败，用户名必须由0~9的数字组成！";
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("用户登录:", ex);
                message = string.Format("登录失败:{0}，请重试！", ex.Message);
            }
            return Json(new { success = result, message = message, defaultUrl = defaultUrl });

        }
    }
}
